package com.chuanyang.driverh.mainhome;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainhome.adapter.SearchDriverAdapter;
import com.chuanyang.driverh.response2.join.ResultSearchList;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class SearchJiaMengActivity extends BaseTitleActivity {
    private ListView lv_list;
    private EditText et_search;
    private ImageView iv_clear_text;

    private String fleet = "";

    private int page = 1;

    private SearchDriverAdapter searchDriverAdapter;
    private List<ResultSearchList.DataBeanX.DataBean> dataBeanList = new ArrayList<>();

    @Override
    protected int getContentResId() {
        return R.layout.activity_search_jia_meng;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        lv_list = (ListView) findViewById(R.id.lv_list);
        et_search = (EditText) findViewById(R.id.et_search);
        et_search.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        et_search.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        et_search.setSingleLine(true);
        iv_clear_text = (ImageView) findViewById(R.id.iv_clear_text);
        iv_clear_text.setOnClickListener(this);

        lv_list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (visibleItemCount + firstVisibleItem == totalItemCount) {
                    View lastVisibleItemView = lv_list.getChildAt(totalItemCount - firstVisibleItem - 1);
                    if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == view.getHeight()) {
                        // 滑动到了底部
                        page = page + 1;
                        if(fleet.equals("1")){//车队
                            search("2",et_search.getText().toString().trim(),page,"10");
                        }else if(fleet.equals("2")){//司机
                            search("1",et_search.getText().toString().trim(),page,"10");
                        }
                    } else {
                    }
                } else {
                }
            }
        });
    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("搜索");

        if(getIntent().getStringExtra("fleet") != null){
            fleet = getIntent().getStringExtra("fleet");
            Log.i("sss","dd"+getIntent().getStringExtra("fleet"));
        }

        et_search.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchJiaMengActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if(et_search.getText().toString().trim() != null){

                        if(fleet.equals("1")){//车队
                            search("2",et_search.getText().toString().trim(),page,"10");
                        }else if(fleet.equals("2")){//司机
                            search("1",et_search.getText().toString().trim(),page,"10");
                        }
                    }else {
                        Toast.makeText(SearchJiaMengActivity.this,"请输入要搜索的内容",Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            }
        });

        searchDriverAdapter = new SearchDriverAdapter(SearchJiaMengActivity.this);
        lv_list.setAdapter(searchDriverAdapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.iv_clear_text:
                et_search.setText("");
                break;
        }
    }
    @Override
    protected void initListener() {

    }

    private void search(String type,String search,int page,String length){
        DialogUtil.showLoadingDialog(SearchJiaMengActivity.this,"加载中...");
        Map<String,Object> paramssss =new HashMap<String, Object>();
        paramssss.put("type", "1");//1：司机 2：车队长
        paramssss.put("fleetId", SharedPrefUtil.getFleetId());
        paramssss.put("search",search);
        paramssss.put("page",page);
        paramssss.put("length",length);

        MyHttpUtil.addBusinessRelation(SearchJiaMengActivity.this,paramssss, new MyCallback<ResultSearchList>(
                SearchJiaMengActivity.this,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {

            }
            @Override
            public void onSuccess(ResultSearchList result, int id) {
                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        dataBeanList.addAll(result.getData().getData());
                        searchDriverAdapter.setNewData(dataBeanList);
                        LogUtils.d(result.getCode()+result.getMsg());
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(SearchJiaMengActivity.this,result.getMsg());
                }
            }
        });
    }
}
